const { exec } = require('child_process');
const ioHook = require('iohook');
const fs = require('fs');
const path = require('path');
var lastDate = new Date().getTime();
ioHook.on('mouseup', (event) => {
  lastDate = new Date().getTime();
});
ioHook.on('keyup', (event) => {
});

ioHook.start();

setInterval(() => {
  var nowDate = new Date().getTime();
  if (!lastDate) {
    lastDate = new Date().getTime();
    return;
  }
  console.log(nowDate - lastDate);
  if (nowDate - lastDate > 1000 * 60 * 15) {
    const isDisable = Number(fs.readFileSync(path.join(__dirname, 'disable.txt')).toString());
    console.log(isDisable);
    if (isDisable == 1) {
      return;
    }
    lastDate = null;
    exec('shutdown -s now', (error, stdout, stderr) => {
        if (error) {
          console.error(`exec error: ${error}`);
          return;
        }
        console.log(`stdout: ${stdout}`);
        console.error(`stderr: ${stderr}`);
    });
  }
}, 5000 * 60);
    